/**
 *
 */
package com.shanghaionstar.activity.webview.handler.picture;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import static android.content.Context.MODE_PRIVATE;

/**
 * @author Administrator
 * @date: 2014-9-4 下午4:10:35
 */
public class SharedPrefsHelp {

    public static final int PRE_MODE = Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE + Context.MODE_MULTI_PROCESS;

    public static final String PREFS_NAME = "demo";// sharedpreferences名字

    public static SharedPreferences sp;

    public static SharedPreferences.Editor editor;

    private static SharedPrefsHelp sharedPrefsHelp = null;


    private SharedPrefsHelp(Application context, String fileName) {
        if (context == null) {
            return;
        }
        if (TextUtils.isEmpty(fileName)) {
            sp = context.getSharedPreferences(context.getPackageName(), MODE_PRIVATE);
        } else
            sp = context.getSharedPreferences(fileName, MODE_PRIVATE);

        editor = sp.edit();
    }


    //初始化
    public static SharedPrefsHelp initSharedPrefsHelp(Application context, String fileName) {
        if (sharedPrefsHelp == null) {
            synchronized (SharedPrefsHelp.class) {
                if (sharedPrefsHelp == null) {
                    sharedPrefsHelp = new SharedPrefsHelp(context, fileName);
                }
            }
        }

        return sharedPrefsHelp;
    }

    public static SharedPrefsHelp getInstance() {
        return sharedPrefsHelp;
    }

    public static SharedPreferences getSharedPrefs() {
        return sp;
    }


    public void setSharedPrefs(String name, String value) {
        editor.putString(name, value);
        editor.commit();
    }

    public String getSharedPrefs(String name, String delfultValue) {
        return sp.getString(name, delfultValue);
    }

    public void setSharedPrefs(String name, long value) {
        editor.putLong(name, value);
        editor.commit();
    }

    public long getSharedPrefs(String name, long delfultValue) {
        return sp.getLong(name, delfultValue);
    }

    public boolean getSharedPrefs(String name, boolean delfultValue) {
        return sp.getBoolean(name, delfultValue);
    }

    public int getSharedPrefs(String name, int delfultValue) {
        return sp.getInt(name, delfultValue);
    }

    public void setSharedPrefs(String name, boolean value) {
        editor.putBoolean(name, value);
        editor.commit();
    }

    public void setSharedPrefs(String name, int value) {
        editor.putInt(name, value);
        editor.commit();
    }

    public void deleteShare(String name) {
        editor.remove(name).commit();
    }
}
